var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		nocMember: {},
        total:0,
        signCount:0,
        showCard: false,
        templates:[],
        templateId: "",
        colObject:{},
        isvip:"",
        btnStr: "群发短信"
	},
    watch: {
        isvip (val,oldval){
            console.log(val);
            vm.nocMember["col15"] = val;
        },
        templateId (val,oldval){
            this.getTemplateTitle();
        },
    },
    created(){
        this.getTemplates();
    },
	methods: {
        getPageSize: function (){
            var winW, winH;
            if(window.innerHeight) {
                winW = window.innerWidth;
                winH = window.innerHeight;
            } else if (document.documentElement && document.documentElement.clientHeight) {
                winW = document.documentElement.clientWidth;
                winH = document.documentElement.clientHeight;
            } else if (document.body) { // other
                winW = document.body.clientWidth;
                winH = document.body.clientHeight;
            }
            return {WinW:winW, WinH:winH};
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.nocMember = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},

        danfa: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var lock = false;
            layer.confirm('确定要给选中的记录发送短信？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                if(!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "noc/nocsendmsg/danfa",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("发送成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function(){
            });
        },

        sendMsg: function (event) {
            if(vm.btnStr=="发送中..."){
                return;
            }
		    vm.btnStr = "发送中...";
            //var ids = getSelectedRows();
            //if(ids == null){
            //    return ;
            //}
            var lock = false;
            layer.confirm('确定要给列表中的记录发送短信？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                if(!lock) {
                    lock = true;
                    vm.nocMember.tid = vm.templateId;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "noc/nocsendmsg/send",
                        contentType: "application/json",
                        data: JSON.stringify(vm.nocMember),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});

                            }else{
                                layer.alert(r.msg);
                            }
                            vm.btnStr = "群发短信2";
                        }
                    });
                }
            }, function(){
            });
        },
		getInfo: function(id){
			$.get(baseURL + "noc/nocmember/info/"+id, function(r){
                vm.nocMember = r.nocMember;
            });
		},
		reload: function (event) {
			vm.showList = true;
            vm.showCard = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			//console.log(page);
            page = 1;
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'col1': vm.nocMember.col1,'col15':vm.nocMember.col15},
                page:page
            }).trigger("reloadGrid");
		},
        sign: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var lock = false;
            layer.confirm('确定要签到选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                if(!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "noc/nocmember/sign",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("签到成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function(){
            });
        },
        memberCount: function () {
            $.get(baseURL + "noc/nocmember/memberCount", function(r){
                console.log(r);
                vm.total = r.total;
                vm.signCount = r.sign;
            });
        },
        sendCard: function () {
            vm.showCard = true;
            vm.showList = false;
            vm.title = "发放入场证件";
            vm.nocMember = {};

            //var ids = getSelectedRows();
            var grid = $("#jqGrid");
            var ids = grid.getGridParam("selarrrow");
            console.log("------");
            console.log(ids);
            if(ids.length>0){
                var rowData = $("#jqGrid").jqGrid('getRowData',ids[0]);
                var col1 = rowData.col1;
                vm.nocMember.col1 = col1;
            }

        },
        saveOrUpdCard: function (event) {
            $('#btnSaveOrUpdCard').button('loading').delay(1000).queue(function() {
                var url = "noc/nocmember/saveCard";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.nocMember),
                    success: function(r){
                        if(r.code === 0){
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdCard').button('reset');
                            $('#btnSaveOrUpdCard').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdCard').button('reset');
                            $('#btnSaveOrUpdCard').dequeue();
                        }
                    }
                });
            });
        },
        getTemplates: function () {
            $.ajax({
                type: "POST",
                url: baseURL + "noc/noctemplate/listByRole",
                contentType: "application/json",
                data: {},
                success: function(r){
                    if(r.code == 0){
                        vm.templates = r.list;
                    }
                }
            });
        },
        getTemplateTitle: function () {
            vm.colObject = {};
            var colModels = new Array();
            $.ajaxSettings.async = false;
            $.get(baseURL + "noc/noctemplatecol/listAll?limit=100&templateId="+vm.templateId, function(r){
                var jqdata = r.list;

                colModels.push($.parseJSON('{ "label": "id", "name": "id", "index": "id", "width": 50, "key": true }'));
                colModels.push($.parseJSON('{ "label": "签到", "name": "isSign", "index": "is_sign", "width": 50 }'));
                colModels.push($.parseJSON('{ "label": "来源", "name": "ly", "index": "ly", "width": 80 }'));
                for(var i=0;i<jqdata.length;i++){
                    if(jqdata[i].is_show=="1") {
                        colModels.push($.parseJSON('{ "label": "'+jqdata[i].cols_name+'", "name": "col' + jqdata[i].col_id + '", "index": "col' + jqdata[i].col_id + '", "width": '+jqdata[i].width+' }'));
                        vm.colObject['col'+jqdata[i].col_id] = true;
                    }
                }
                colModels.push($.parseJSON('{ "label": "签到时间", "name": "signTime", "index": "signTime", "width": 150 }'));
                colModels.push($.parseJSON('{ "label": "创建时间", "name": "createTime", "index": "create_time", "width": 150 }'));
                colModels.push($.parseJSON('{ "label": "修改人", "name": "updator", "index": "updator", "width": 150 }'));
                colModels.push($.parseJSON('{ "label": "修改时间", "name": "updateTime", "index": "update_time", "width": 150 }'));
            });
            console.log("-------------------");
            console.log(vm.colObject);
            $("#jqBlock").html("");
            $("#jqBlock").append('<table id="jqGrid"></table>');
            $("#jqBlock").append('<div id="jqGridPager"></div>');
            $("#jqGrid").jqGrid({
                url: baseURL + 'noc/nocmember/list',
                datatype: "json",
                colModel: colModels,
                viewrecords: true,
                height: 430,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                shrinkToFit:false,
                autoScroll: true,
                pager: "#jqGridPager",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                    var ss = vm.getPageSize();
                    $("#jqGrid").jqGrid('setGridWidth', ss.WinW-10).jqGrid('setGridHeight', ss.WinH-130);
                },
                loadComplete: function(data){
                    //设置主活动
                    var obj = $("#jqGrid");
                    var rowIds = obj.getDataIDs();
                    if (rowIds.length > 0) {
                        for (var i = 0; i < rowIds.length; i++) {
                            console.log(obj.getRowData(rowIds[i]));
                            var rowData = obj.getRowData(rowIds[i]);
                            if(rowData.id==vm.memberId){
                                obj.setSelection(rowIds[i]);
                            }
                        }
                    }
                }
            });

            var page = $("#jqGrid").jqGrid('getGridParam','page');
            page = 1;
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'col1': vm.nocMember.col1,'col2': vm.nocMember.col2,'ly': vm.ly,'templateId':vm.templateId},
                page:page
            }).trigger("reloadGrid");
        }
	}
});


